/*
 * Main window of Lukija RiL client.
 *
 * @author Yves Marcoz
 *
 * Copyright(C) 2011 Yves Marcoz <yvez@marcoz.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import Qt 4.7
import "ril.js" as RIL
import "storage.js" as Storage
import "toolbar" as ToolBar

Item {
    id: screen
    width: 480
    height: 360

    Rectangle {
        color: "#343434";
        id: container
        anchors.centerIn: parent
        transformOrigin: Item.Center

        Component.onCompleted: {
            Storage.initialize();
            if (Storage.getSetting("apikey") === "Unknown") {
                RIL.rilDownloadApiKey();
            }
            if (Storage.getSetting("username") === "Unknown") {
                loginPage.makeVisible();
            }
        }

        Item {
            width: parent.width;
            height: parent.height - toolBar.height;
            anchors.top: toolBar.bottom; anchors.bottom: parent.bottom

            ArticleViewer {
                id: articleViewer;
                visible: true;

                /* Refresh article list model against database.
                 *
                 * This is, add new articles and update existing rows.
                 * Item removal is *not* supported however. */
                function refreshList() {
                    RIL.rilGet(articles);
                }

                /* Remove read articles from the article list model. */
                function removeReadArticles() {
                    for(var i = 0; i < articles.count; ) {
                        if(+articles.get(i).unread) {
                            articles.remove(i);
                        } else {
                            i ++;
                        }
                    }
                }

                Component.onCompleted: {
                    articles.clear();
                    refreshList();
                }
            }

            Login {
                id: loginPage;
            }
        }

        ToolBar.ToolBar {
            id: toolBar; z: 7
            height: 66; anchors.top: parent.top; width: parent.width; opacity: 0.9

            onTaskSwitcherClicked: {
                utility.taskSwitcher();
            }

            onBackClicked: {
                if (loginPage.visible == true) {
                    loginPage.makeHidden();
                } else {
                    if (articleViewer.visible == true) {
                        articleViewer.back();
                    } else {
                        Qt.quit();
                    }
                }
            }

            onMenuClicked: loginPage.makeVisible();

            onUpdateClicked: {
                    toolBar.feedUpdating = true;
                    RIL.updateList();
                    articleViewer.refreshList();
            }

            onMarkAllClicked: {
                Storage.markRilArticleAsRead(articleViewer.getCurrentUrl());
                articleViewer.refreshList();
                notification.showNotification("Article marked as read.");
            }

            states: [ State {
                name: "navButtons"; when: articleViewer.articleShown
                PropertyChanges { target: toolBar; markAllVisible: true; }
                PropertyChanges { target: toolBar; updateVisible: false; }
                PropertyChanges { target: toolBar; backVisible: true; }
            },
                State {
                    name: "feedButtons"; when: (articleViewer.visible)&&(!articleViewer.articleShown)
                    PropertyChanges { target: toolBar; quitVisible: true;}
                    PropertyChanges { target: toolBar; backVisible: false; }
                    PropertyChanges { target: toolBar; updateVisible: true; }
                },
                State {
                    name: "quitButton"; when: (!articleViewer.articleShown)
                    PropertyChanges { target: toolBar; quitVisible: true;}
                    PropertyChanges { target: toolBar; backVisible: false; }
                    PropertyChanges { target: toolBar; updateVisible: true; }
                }
            ]

        }

        Notification {
            id: notification
        }

        property bool lockRotation: false
        property variant oldOrientation: "Portrait"
        state: utility.state
        property bool inPortrait: (state == "Portrait" || state == "PortraitInverted");

        // rotation correction for landscape devices like N900
        property bool landscapeWindow: screen.width > screen.height
        property variant rotationDelta: landscapeWindow ? -90 : 0
        rotation: rotationDelta

        // initial state is portrait
        property real baseWidth: landscapeWindow ? screen.height : screen.width
        property real baseHeight: landscapeWindow ? screen.width : screen.height

        width: baseWidth
        height: baseHeight

        function getAngle(orientation) {
            var angle;
            if (orientation == "Portrait") {
                angle = 0;
            } else if (orientation == "Landscape") {
                angle = 90;
            } else if (orientation == "PortraitInverted") {
                angle = 180;
            } else if (orientation == "LandscapeInverted") {
                angle = 270;
            } else {
                angle = getAngle(oldOrientation);
            }
            return angle;
        }

        states: [
            State {
                name: "Landscape"
                PropertyChanges {
                    target: container
                    rotation: getAngle("Landscape")+rotationDelta
                    width: baseHeight
                    height: baseWidth
                }
            },
            State {
                name: "PortraitInverted"
                PropertyChanges {
                    target: container
                    rotation: getAngle("PortraitInverted")+rotationDelta
                    width: baseWidth
                    height: baseHeight
                }
            },
            State {
                name: "LandscapeInverted"
                PropertyChanges {
                    target: container
                    rotation: getAngle("LandscapeInverted")+rotationDelta
                    width: baseHeight
                    height: baseWidth
                }
            },
            State {
                name: "Portrait"
                PropertyChanges {
                    target: container
                    rotation: getAngle("Portrait")+rotationDelta
                    width: baseWidth
                    height: baseHeight
                }
            },
            State {
                name: "orientation 0"
                PropertyChanges {
                    target: container
                    rotation: getAngle(container.oldOrientation)+rotationDelta
                    width: container.inPortrait ? baseWidth : baseHeight
                    height: container.inPortrait ? baseHeight : baseWidth
                }
            }
        ]
        transitions: Transition {
            ParallelAnimation {
                RotationAnimation {
                    direction: RotationAnimation.Shortest
                    duration: 300
                    easing.type: Easing.InOutQuint
                }
                NumberAnimation {
                    properties: "x,y,width,height"
                    duration: 300
                    easing.type: Easing.InOutQuint
                }
            }
        }
    }
}
